﻿using LeRenard.Libraries.PlatformInspection.InfoNodes;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LeRenard.Libraries.PlatformInspection.Inspectors.Environment
{
    public class SpecialDirectoriesEnvironmentInspectorModule : InspectorModule<EnvironmentInspector>
    {
        /// <summary>
        /// Constructions
        /// </summary>
        public SpecialDirectoriesEnvironmentInspectorModule() 
            : base("Special Directories") 
        { 
        }

        /// <summary>
        /// Gets the information this module gathers.
        /// </summary>        
        protected override IEnumerable<InfoNode> GetInformationImpl()
        {
            var infoNodes = new List<InfoNode>();

            var specialFolderNames = Enum.GetNames(typeof(System.Environment.SpecialFolder));
            foreach (var specialFolderName in specialFolderNames)
            {
                try
                {
                    var specialFolder = (System.Environment.SpecialFolder) Enum.Parse(typeof(System.Environment.SpecialFolder), specialFolderName);
                    var specialFolderValue = System.Environment.GetFolderPath(specialFolder);

                    var displayStrategy = InfoNodeEmptyValueDisplayStrategy.TreatAsNormalInfoNode;
                    if (String.IsNullOrEmpty(specialFolderValue))
                        displayStrategy = InfoNodeEmptyValueDisplayStrategy.TreatAsNormalInfoNodeWithoutValue;

                    infoNodes.Add(new InfoNode
                        (
                            name: (string) specialFolderName,
                            value: specialFolderValue,
                            displayStrategy: displayStrategy
                        )
                    );
                }
                catch (Exception ex)
                {
                    var exception = new Exception("Could not get value for special folder '" + specialFolderName + "'.", ex);
                    _exceptions.Add(exception);
                }
            }

            return infoNodes.OrderBy(inf => inf.Name);
        }
    }
}
